﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StoreWeb.system.basis
{
    public partial class Inventory : System.Web.UI.Page
    {
        DataSet ds = new DataSet();
        Maticsoft.BLL.InventoryClass blls = new Maticsoft.BLL.InventoryClass();
        Maticsoft.BLL.Inventory bll = new Maticsoft.BLL.Inventory();
        Maticsoft222.BLL.sqlexec sbll = new Maticsoft222.BLL.sqlexec();
        Maticsoft.BLL.Dictionary dbll = new Maticsoft.BLL.Dictionary();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["id"] != "0" && !string.IsNullOrEmpty(Request["id"]))
                {
                    string where = "ptype=1 and cInvCCode like '01%' and cInvCCode like '" + Request["id"] + "%'";
                    hidwhere.Value = where;

                    List<Maticsoft.Model.InventoryClass> list = blls.GetModelList("cInvCCode='" + Request["id"] + "'");
                    if (list.Count > 0 && list[0].bInvCEnd)
                        ltsetprice.Text = "<a href=\"javascript:;\" onclick=\"setprice('设置吊牌价','setprice.aspx','" + list[0].id + "','500','240')\" class=\"btn btn-secondary radius\"><i class=\"Hui-iconfont\">&#xe63a;</i> 设置价格</a> ";
                }
                else
                {
                    string where = "ptype=1 and cInvCCode like '01%'";
                    hidwhere.Value = where;
                }
                //this.typelist.DataSource = blls.GetList("(cInvCCode like '01%' or cInvCCode like '02%')");
                this.typelist.DataSource = sbll.exsql("select cInvCCode as parentcode,case when iinvcgrade =1 then '┊┈'+cInvCName  when iinvcgrade =2 then '┊┈┊┈'+cInvCName when iinvcgrade =3 then '┊┈┊┈┊┈'+cInvCName when iinvcgrade =4 then '┊┈┊┈┊┈┊┈'+cInvCName end as parentname from dbo.InventoryClass where (cInvCCode like '01%') order by cinvccode");
                //this.typelist.DataSource = sbll.exsql("select cInvCCode as parentcode,case when iinvcgrade =1 then '┊┈'+cInvCName  when iinvcgrade =2 then '┊┈┊┈'+cInvCName when iinvcgrade =3 then '┊┈┊┈┊┈'+cInvCName when iinvcgrade =4 then '┊┈┊┈┊┈┊┈'+cInvCName end as parentname from dbo.InventoryClass where (cInvCCode like '01%' or cInvCCode like '02%') order by cinvccode");
                this.typelist.DataTextField = "name";
                this.typelist.DataValueField = "code";
                this.typelist.DataBind();
                this.typelist.Items.Insert(0, new ListItem("--请选择物料类型--", ""));

                this.parentlist.DataSource = dbll.GetList("parentcode='PO_POCLASS'");
                this.parentlist.DataTextField = "name";
                this.parentlist.DataValueField = "code";
                this.parentlist.DataBind();
                this.parentlist.Items.Insert(0, new ListItem("--请选择采购分类--", ""));

                Maticsoft222.DBUtility.TreeHelper th = new Maticsoft222.DBUtility.TreeHelper();
                //List<TreeNode> allNodes = th.GetAllNodes(blls.GetList("(cInvCCode like '01%' or cInvCCode like '02%')").Tables[0], "cInvCCode", "cInvCName");
                List<TreeNode> allNodes = th.GetAllNodes(blls.GetList("(cInvCCode like '01%')").Tables[0], "cInvCCode", "cInvCName");
                th.AutoTreeView(tv, allNodes);//加载树
                int totalOrders = bll.GetRecordCount(hidwhere.Value);
                AspNetPager1.RecordCount = totalOrders;
            }
        }
        void bindData(string where)
        {
            Repeater1.DataSource = bll.GetListByPage(string.IsNullOrEmpty(where) ? "ptype=1 and cInvCCode like '01%'" : where, "cInvCode", AspNetPager1.StartRecordIndex, AspNetPager1.EndRecordIndex);
            Repeater1.DataBind();
        }

        protected void AspNetPager1_PageChanged(object src, EventArgs e)
        {
            bindData(hidwhere.Value);
        }
        protected string gettypename(string code)
        {
            if (string.IsNullOrEmpty(code))
                return "";
            else
            {
                List<Maticsoft.Model.InventoryClass> list = blls.GetModelList("cInvCCode='" + code + "'");
                if (list != null && list.Count > 0)
                    return list[0].cInvCName + "/" + list[0].price;
                else
                    return "/";
            }
        }
        protected string getdictionaryname(string code)
        {
            if (string.IsNullOrEmpty(code))
                return "--";
            else
            {
                List<Maticsoft.Model.Dictionary> list = dbll.GetModelList("code='" + code + "'");
                return list[0].name;
            }
        }
        protected void btnserach_Click(object sender, EventArgs e)
        {
            string where = "ptype=1  and cInvCCode like '01%'";
            if (rdobtnbuy.SelectedItem != null && rdobtnbuy.SelectedItem.Value != "")
            {
                where += " and buyvalue='" + rdobtnbuy.SelectedItem.Value + "'";
            }
            if (parentlist.SelectedItem.Value != "")
            {
                where += " and dictionarycode='" + parentlist.SelectedItem.Value + "'";
            }
            if (typelist.SelectedItem.Value != "")
            {
                where += " and cInvCCode like '" + typelist.SelectedItem.Value + "%'";
            }
            if (name.Value != null && !string.IsNullOrEmpty(name.Value))
            {
                where += " and cInvName like '%" + name.Value + "%'";
            }
            hidwhere.Value = where;
            bindData(hidwhere.Value);
        }
    }
}